package com.user.dbdao;

import com.data.dbdao.BaseDao;
import com.user.bean.*;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 软件上传数据管理层
 *
 * @author:HingLo
 * @create 2017-09-15 9:53
 **/
@Repository
@Transactional
public class SoftwareDao extends BaseDao {

    public static void main(String[] args) {
        String s = "helloworld";
        System.out.println(s.substring(0, s.length() - 1));
    }

    /**
     * 查询全部提示标签信息
     *
     * @return
     */
    public List<?> findSoftwareDao() {
        String hql = "SELECT keywordtype.name,keyword.name,keyword.id from KeywordType as keywordtype ,KeyWord as keyword where keyword.type=keywordtype.id";
        return super.findMassageDao(hql);
    }

    /**
     * 通过传入软件的md5值，检测该软件是否在我们的熟库中已经存在
     * 注意，软件的id是用软件的md5值作为主键
     *
     * @param packageName
     * @return
     */
    public boolean checkApkExistenceDao(String packageName) {
        String hql = "from App where packagename=?";
        return super.findMassageDao(hql, packageName).size() > 0;
    }

    public boolean saveSoftwrreMassage(App app, AppDetail appDetail, AppDownload appDownload, List<AppIcon> list, List<AppMenu> menuList) {
        try {
            super.saveMassageDao(app, appDetail, appDownload);
            for (AppIcon a : list) {
                super.saveMassageDao(a);

            }
            for (AppMenu m : menuList) {
                super.saveMassageDao(m);
            }
            return true;
        } catch (Exception e) {
            return false;
        }


    }

    /**
     * 删除未通过或者审核中的APP
     *
     * @param id
     * @return
     */
    public boolean deleteSoftwareService(String id) {
        String hql = "delete App  where id=? and state!=?";
        return super.updateManyMssageDao(hql, id, 1) > 0;
    }

    /***
     * 申请下线，但是申请下线的状态必须是在线状态的软件
     * @param id
     * @param
     * @return
     */
    public boolean downOfflineSoftwareDao(String id, AppOffline appOffline) {
        String hql = "update App set state=? where id=? and state=?";
        if (super.updateOneMssageDao(hql, 2, id, 1)) {
            return super.saveMassageDao(appOffline);
        }
        return false;
    }

    /**
     * //软件基本信息
     *
     * @param id
     * @return
     */
    public List<?> findSoftwareInfoDao(String id) {
        String hql = "SELECT a.name,a.type,m.name,a.person,a.content,a.introduce,a.md5,a.version,a.vcontent,app.resolving,app.lang,app.charge,app.equipment from App as a ,Menu as m,AppDetail as app where a.id=? and m.id=a.menuid and app.sid=a.id";
        return super.findMassageDao(hql, id);

    }

    /**
     * //软件详情信息
     *
     * @param id
     * @return
     */
    public List<?> findSoftwareIconDao(String id) {
        String hql = "SELECT a.rid,a.name,a.types from AppIcon as a where a.rid=?";
        return super.findMassageDao(hql, id);
    }

    /**
     * //软件菜单分类信息
     *
     * @param id
     * @return
     */
    public List<?> findSoftwareMenuDao(String id) {
        String hql = "SELECT m.name FROM AppMenu as a ,Menu as m  WHERE a.rid=? and a.mid=m.id";
        return super.findMassageDao(hql, id);
    }

    /**
     * 更新软件信息
     *
     * @param list
     * @param rid
     * @param uid
     * @param object
     * @return
     */
    public boolean softwareUpdateUpload(List<AppIcon> list, String rid, String uid, Object... object) {

        String hql = "update App set name=?, content=?, introduce=?, vcontent=?, size=?, version=?, md5=?, state=? where id=? and uid=?";
        boolean bool = updateOneMssageDao(hql, object);
        if (bool) {
            if (list == null || list.isEmpty()) {
                return bool;
            }
            List<String> res = new ArrayList<>();
            list.forEach(x -> res.add(x.getTypes()));

            Set<String> newList = new HashSet(res);

            StringBuilder hql1 = new StringBuilder("delete AppIcon where rid=? and uid=? and types in (");

            newList.forEach(action -> hql1.append("?,"));
            hql = hql1.substring(0, hql1.length() - 1);
            hql += ")";
            Object[] o = new Object[newList.size() + 2];
            o[0] = rid;
            o[1] = uid;

            int i = 2;
            for (String ares : newList) {
                o[i] = ares;
                i++;
            }
            //删除软件图片
            int ex = updateManyMssageDao(hql, o);
            if (list.size() > 0) {
                //保存新的图片
                Object[] arrays = list.toArray();
                return super.saveMassageDao(arrays);
            }
            return true;

        }
        return false;

    }

    /**
     * 升级操作，查看软件包是否对应的存在在我们的系统中
     *
     * @param packageName
     * @param rid
     * @return
     */
    public boolean checkApkExistenceDao(String packageName, String rid) {
        String hql = "from App where packagename=? and id=?";
        return super.findMassageDao(hql, packageName, rid).size() > 0 ? true : false;
    }

    public App findSoftwareDao(String rid) {
        return this.getHibernateTemplate().get(App.class, rid);

    }
}
